﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Diagnostics;

namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for SharedMemoryStreamTest and is intended
    ///to contain all SharedMemoryStreamTest Unit Tests
    ///</summary>
	[TestClass()]
	public class SharedMemoryStreamTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Position
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void PositionTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			long expected = 0;
			long actual;
			target.Position = expected;
			actual = target.Position;
			Assert.AreEqual(expected, actual);
			target.WriteByte(1);
			expected++;
			actual = target.Position;
			Assert.AreEqual(expected, actual);

			target.Close();
		}

		/// <summary>
		///A test for Length
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void LengthTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			long actual;
			actual = target.Length;

			Assert.AreEqual(size, actual);

			target.Close();
		}

		/// <summary>
		///A test for CanWrite
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanWriteTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			bool actual;
			actual = target.CanWrite;
			Assert.IsTrue(actual);
			target.Close();
		}

		/// <summary>
		///A test for CanSeek
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanSeekTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			bool actual;
			actual = target.CanSeek;
			Assert.IsTrue(actual);
			target.Close();
		}

		/// <summary>
		///A test for CanRead
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanReadTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			bool actual;
			actual = target.CanRead;
			Assert.IsTrue(actual);
			target.Close();
		}

		/// <summary>
		///A test for Write
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void WriteTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 2;
			int count = 2;
			target.Write(buffer, offset, count);

			target.Seek(0, SeekOrigin.Begin);
			byte [] data = new byte[size];
			target.Read(data, 0, size);

			Assert.AreEqual(data[0], buffer[2]);
			Assert.AreEqual(data[1], buffer[3]);
			Assert.AreNotEqual(data[2], buffer[4]);

			target.Close();
		}

		/// <summary>
		///A test for SetLength
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void SetLengthTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			try
			{
				target.SetLength(1);
				Assert.Fail("Should have thrown an exception.");
			}
			catch (IPCException ipce)
			{
				Trace.WriteLine(ipce);
			}
			target.Close();
		}

		/// <summary>
		///A test for Seek
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void SeekTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			long offset = 1;
			long expected = 1;
			long actual;
			actual = target.Seek(offset, SeekOrigin.Begin);
			Assert.AreEqual(expected, actual);

			actual = target.Seek(offset, SeekOrigin.Current);
			Assert.AreEqual(2, actual);

			actual = target.Seek(offset, SeekOrigin.End);
			Assert.AreEqual(size-1, actual);

			target.Close();
		}

		/// <summary>
		///A test for Read
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void ReadTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 0;
			int count = buffer.Length;
			target.Write(buffer, offset, count);

			target.Seek(0, SeekOrigin.Begin);
			byte[] data = new byte[size];
			target.Read(data, 4, 2);

			Assert.AreEqual(data[4], buffer[0]);
			Assert.AreEqual(data[5], buffer[1]);
			Assert.AreNotEqual(data[6], buffer[2]);

			target.Close();
		}

		/// <summary>
		///A test for Flush
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void FlushTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);
			Stream target = sm.GetStream();

			target.Flush();
			target.WriteByte(1);
			target.Flush();
			target.Position = 0;
			int b = target.ReadByte();
			Assert.AreEqual(b, 1);

			target.Close();
		}

		/// <summary>
		///A test for Flush
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void ReadWriteDataTest()
		{
			string moduleName = "data.dll";
			string name = "data";
			int size = 8192;
			SharedMemory sm = new SharedMemory(moduleName, name, size);

			BinaryReader br = new BinaryReader(sm.GetStream());
			BinaryWriter bw = new BinaryWriter(sm.GetStream());

			bw.Write(true);
			bw.Write(1234);
			bw.Write(1.234f);
			bw.Write(1.234);

			Assert.AreEqual(true, br.ReadBoolean());
			Assert.AreEqual(1234, br.ReadInt32());
			Assert.AreEqual(1.234f, br.ReadSingle());
			Assert.AreEqual(1.234, br.ReadDouble());

			br.Close();
			bw.Close();
		}

	}
}
